package com.skyjing.toolsuitls.java.base;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.KeyEvent;

import com.skyjing.toolsuitls.R;
import com.skyjing.toolsuitls.kotlin.tools.SingleToast;

/**
 * Created by Administrator on 2018/12/25 16:07
 *
 * @author Administrator
 **/
public class BaseActivity extends AppCompatActivity {
    protected static Resources resources;
    private long mExitTimer;
    protected boolean isOpenKeyListener = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        resources = this.getResources();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public int getmColor(int resId) {
        return ContextCompat.getColor(this, resId);
    }

    protected void startActivitys(Intent intent){
        startActivity(intent);
        overridePendingTransition(R.anim.in_from_right, R.anim.out_from_left);
    }

    protected void startActivitys(Intent intent , int enterAnim, int exitAnim){
        startActivity(intent);
        overridePendingTransition(enterAnim, exitAnim);
    }

    protected void startActivityForResults(Intent intent , int requestCode ,  int enterAnim, int exitAnim){
        startActivityForResult(intent, requestCode);
        overridePendingTransition(enterAnim, exitAnim);
    }

    protected void finishActivity(Activity activity){
        activity.finish();
        overridePendingTransition(R.anim.in_from_left , R.anim.out_to_right);
    }

    protected void finishActivity(Activity activity, int enterAnim, int exitAnim){
        activity.finish();
        overridePendingTransition(enterAnim , exitAnim);
    }



    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            default:break;
            case KeyEvent.KEYCODE_BACK:
                if (isOpenKeyListener) {
                    if ((System.currentTimeMillis() - mExitTimer) > 2000) {
                        SingleToast.Companion.getInstance().show(R.string.back_btn_tips, 100, Gravity.CENTER);
                        mExitTimer = System.currentTimeMillis();
                    } else {
                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_MAIN);
                        intent.addCategory(Intent.CATEGORY_HOME);
                        startActivity(intent);
                        finish();
                    }
                } else {
                    this.finish();
                    overridePendingTransition(R.anim.in_from_left , R.anim.out_to_right);
                }
                return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
